/*$Copyright:
 * Copyright (C) 1992.5.22. Recruit Co.,Ltd. 
 * Institute for Supercomputing Research
 * All rights reserved.
 * NewsBase  by ISR, Kazuto MIYAI, Gary ARAKAKI, Katsunori SUZUKI, Kok-meng Lue
 *
 * You may freely copy, distribute and reuse the code in this program under 
 * following conditions.
 * - to include this notice in the source code, if it is to be distributed 
 *   with source code.
 * - to add the file named "COPYING" within the code, which shall include 
 *   GNU GENERAL PUBLIC LICENSE(*).
 * - to display an acknowledgement in binary code as follows: "This product
 *   includes software developed by Recruit Co.,Ltd., ISR."
 * - to display a notice which shall state that the users may freely copy,
 *   distribute and reuse the code in this program under GNU GENERAL PUBLIC
 *   LICENSE(*)
 * - to indicate the way to access the copy of GNU GENERAL PUBLIC LICENSE(*)
 *
 *   (*)GNU GENERAL PUBLIC LICENSE is stored in the file named COPYING
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
$*/
/* IBaseControl.m */

#import <appkit/appkit.h>
#import <mach/mach_init.h>
#import <defaults/defaults.h>
#import <libc.h>
#import "IBaseControl.h"
#import "INewsBase.h"
#import "IIOmodule.h"
#import "TransparentWindow.h"
#import "IDocTransparentWindow.h"
#import "data_types.h"
#import "errdebug.h"
#import "IReceiveSpeaker.h"
#import "IReceiveListener.h"
#import "Localization.h"
#import "IHelp.h"
#import "InfoPanel.h"

#define LoStr(key)      doLocalString(NULL,key,NULL)

struct windowDef {
    @defs(Window)
};

@implementation IBaseControl

- appWillInit:sender
{
    [NXApp setAppSpeaker:[[IReceiveSpeaker alloc] init]];
    [NXApp setAppListener:[[IReceiveListener alloc] init]];
    return(self);
}

- appDidInit:sender
{
    iHelpPanel = nil;
    iInfoPanel = nil;
    
    [oMenuTitle setTitle:"NewsBase3.0"];
    if ((nntpBase = [[INewsBase alloc] init]) == nil) {
        return(nil);
    }
    [nntpBase show:self];
    return(self);
}

- appWillTerminate:sender
{
    int n;
    Window *window;
    List *windowList;
    Class isA;

    [nntpBase terminateProc];
    
    DBG(1, [self debug];);
    windowList = [NXApp windowList];
    for (n = 0; window = [windowList objectAt:n]; ++n) {
        if (NXZoneFromPtr(window) == 0) {
            DBG(1, fprintf(stderr, "NXZoneFromPtr(window = %#x) = %#x\n",
                (unsigned int)window, (unsigned int)NXZoneFromPtr(window)););
            [windowList removeObject:window];
            --n;
            continue;
        }
        if ((isA = ((struct windowDef *)window)->isa) != (Class)[Window class]
            && isA != (Class)[Panel class] && isA != (Class)[Menu class] &&
            isA != (Class)[PopUpList class]) {
            DBG(1, fprintf(stderr, "(window = %#x)->isa = %#x\n",
                (unsigned int)window, (unsigned int)((struct windowDef *)window)->isa););
            [windowList removeObject:window];
            --n;
        }
    }
    // remove all temporary files
    return(self);
}

- openViewControlPanel:sender
{
    if ([nntpBase respondsTo:@selector(openViewControlPanel:)]) {
	[nntpBase openViewControlPanel:self];
	return self;
    } else {
	return nil;
    }
}

- browserDidBecomeKey:sender
{
//    [oBrowserControlMenuCell setEnabled:YES];
    if ([sender class] == [INewsBase class]) {
	[oUnscribeNewsgroupMenuCell setEnabled:YES];
	[oUnscribeNewsgroupMenuCell setTarget:sender];
	[oUnscribeNewsgroupMenuCell setAction:@selector(unSubscribeGroup:)];
	
	[oShowAllArticleMenuCell setEnabled:YES];
	[oShowAllArticleMenuCell setTarget:sender];
	[oShowAllArticleMenuCell setAction:@selector(setReadFlagToAll:)];
	
	[oSortBySubjectMenuCell setEnabled:YES];
	[oSortBySubjectMenuCell setTarget:sender];
	[oSortBySubjectMenuCell setAction:@selector(toggleArticleBrowser:)];
	
	[oSortByKeywordMenuCell setEnabled:YES];
	[oSortByKeywordMenuCell setTarget:sender];
	[oSortByKeywordMenuCell setAction:@selector(toggleArticleBrowser:)];
	
	[oArticleBrowserMenuCell setEnabled:YES];
	[oArticleBrowserMenuCell setTarget:sender];
	[oArticleBrowserMenuCell setAction:@selector(toggleArticleBrowser:)];
	
	[oArticleListMenuCell setEnabled:YES];
	[oArticleListMenuCell setTarget:sender];
	[oArticleListMenuCell setAction:@selector(toggleArticleBrowser:)];
	
	[oNewsgroupBrowserMenuCell setEnabled:YES];
        [oNewsgroupBrowserMenuCell setTarget:sender];
	[oNewsgroupBrowserMenuCell
				 setAction:@selector(toggleNewsGroupBrowser:)];
				 
	[oNewsgroupListMenuCell setEnabled:YES];
        [oNewsgroupListMenuCell setTarget:sender];
	[oNewsgroupListMenuCell setAction:@selector(toggleNewsGroupBrowser:)];

	[oMarkAllArticleMenuCell setEnabled:YES];
        [oMarkAllArticleMenuCell setTarget:sender];
	[oMarkAllArticleMenuCell setAction:@selector(markAllArticleAsRead:)];

	[oNewArticlesMenuCell setEnabled:YES];
        [oNewArticlesMenuCell setTarget:sender];
	[oNewArticlesMenuCell setAction:@selector(getNewArticles:)];
    }
    return(self);
}

- browserDidResignKey:sender
{
    [oUnscribeNewsgroupMenuCell setEnabled:NO];
    [oShowAllArticleMenuCell setEnabled:NO];
    [oSortBySubjectMenuCell setEnabled:NO];
    [oSortByKeywordMenuCell setEnabled:NO];
    [oArticleBrowserMenuCell setEnabled:NO];
    [oArticleListMenuCell setEnabled:NO];
    [oNewsgroupBrowserMenuCell setEnabled:NO];
    [oNewsgroupListMenuCell setEnabled:NO];
    [oMarkAllArticleMenuCell setEnabled:NO];
    [oNewArticlesMenuCell setEnabled:NO];
//    [oBrowserControlMenuCell setEnabled:NO];
    return(self);
}

-(int)receiveArticle:(char *)article length:(int)length
{
//    fprintf(stderr, "%80s\n", article);
    [nntpBase postArticle:article length:length];
    return(0);
}

- (int)sendArticleWithMessageID:(const char *)messageID
{
    [nntpBase sendArticle:messageID];
    return(0);
}

- (int)makeMimeIconWindowAtpointX:(int)x pointY:(int)y 
			offsetX:(int)offx offsetY:(int)offy
			sourceWindow:(int)wnum
{
    id		iconWindow;
    id		image;
    NXPoint	originPoint;
    NXPoint	offset;

    originPoint.x = x;
    originPoint.y = y;
    image = [NXImage findImageNamed:MIMEDOCICON];
    iconWindow = [[IDocTransparentWindow
			allocFromZone:[self zone]] 
			initForImage:image
			at:&originPoint
			forView:NULL];
    [iconWindow setSourceWindowNum:wnum];
    offset.x = offx;
    offset.y = offy;
    
    [NXApp activateSelf:YES];
    [iconWindow dragFromMouseDown:&originPoint mouseOffset:&offset];
    return (0);
}

- setOMainMenu:mainMenu
{
    oMainMenu = mainMenu;
    return(self);
}

- openHelpPanel:sender
{
    if (iHelpPanel == nil) {
	iHelpPanel = [[IHelp allocFromZone:[self zone]] init];
    }
    [iHelpPanel openHelpPanel:sender];
    return self;
}

- openInfoPanel:sender
{
    if (iInfoPanel == nil) {
	iInfoPanel = [[InfoPanel allocFromZone:[self zone]] init];
    }
    [iInfoPanel openInfoPanel:sender];
    return self;
}

#ifdef DEBUG

- (void)debug
{
    DBG(1, fprintf(stderr, "NXMallocCheck() = %d\n", NXMallocCheck()););
    DBG(1, {
        int *screenWindowList;
        int screenWindowListCount;
        List *windowList;
        int n;
        Window *window;
        int windowNum;
        NXRect rect;
        BOOL isVisible;
        NXZone *zone;
        char response[256];
        Class isA;

        fprintf(stderr, 
	  "NXDefaultMallocZone() = %x\n", (unsigned int)NXDefaultMallocZone());
        fprintf(stderr, "Windows for this Application\n");
        [NXApp getWindowNumbers:&screenWindowList 
				count:&screenWindowListCount];
        fprintf(stderr, 
	  "screen window count = %d\n", (int)screenWindowListCount);
        for (n = 0; n < screenWindowListCount; ++n) {
            fprintf(stderr, "%d ", (int)screenWindowList[n]);
        }
        fprintf(stderr, "\n");
        windowList = [NXApp windowList];
        fprintf(stderr, "window count = %d\n", (int)[windowList count]);
        for (n = 0; window = [windowList objectAt:n]; ++n) {
            if (NXZoneFromPtr(window) == 0) {
                fprintf(stderr, "NXZoneFromPtr(window = %#x) = %#x\n",
                    (unsigned int)window, (unsigned int)NXZoneFromPtr(window));
                continue;
            }
            if ((isA = ((struct windowDef *)window)->isa) != [Window class] &&
                isA != [Panel class] && isA != [Menu class] &&
                isA != [PopUpList class]) {
                fprintf(stderr, "(window = %#x)->isa = %#x\n",
                    (unsigned int)window, 
		    (unsigned int)((struct windowDef *)window)->isa);
                continue;
            }
            windowNum = [window windowNum];
            [window getFrame:&rect];
            isVisible = [window isVisible];
            zone = [window zone];
            fprintf(stderr, "window = %x, window number = %2d, zone = %x, "
                "width = %7.0f, height = %7.0f, "
                "is visible %s\n",
                (unsigned int)window, (int)windowNum, 
		(unsigned int)zone, 
		rect.size.width, rect.size.height,
                isVisible == YES ? "YES" : "NO");
        }
        for (n = 0; window = [windowList objectAt:n]; ++n) {
            if (NXZoneFromPtr(window) == 0) {
                fprintf(stderr, "NXZoneFromPtr(window = %#x) = %#x\n",
                    (unsigned int)window, (unsigned int)NXZoneFromPtr(window));
                continue;
            }
            if ((isA = ((struct windowDef *)window)->isa) != [Window class] &&
                isA != [Panel class] && isA != [Menu class] &&
                isA != [PopUpList class]) {
                fprintf(stderr, "(window = %#x)->isa = %#x\n",
                    (unsigned int)window, 
		    (unsigned int)((struct windowDef *)window)->isa);
                continue;
            }
            windowNum = [window windowNum];
            isVisible = [window isVisible];
            if (windowNum > 0 && isVisible == NO) {
                fprintf(stderr, "orderFront: sent to window number %d"
                    " return to continue > ", (int)windowNum);
                [window orderFront:self];
                NXPing();
		fgets(response, sizeof(response), stdin);
                [window orderOut:self];
            }
        }
    });
}

#endif

@end
